$(function () {
    var type=GetQueryString("type");
    if(type!=null){
        if(type==1){
            load(null,"瓷器");
        }
        if(type==2){
            load(null,"玉器");
        }
        if(type==3){
            load(null,"字画");
        }
        if(type==4){
            load(null,"钱币");
        }
    }else {
        load(null);
    }

});
function GetQueryString(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}
function sousuo(){
    var sname=$("#sousuoa").val();
    load(null,null,sname);
}

function load(currentPage,type,sname){
    sname=$("#sousuoa").val();

    // <li>
    // <a href="detail.jsp" class="ccsl">字画装裱需要注意什么细节问题?</a>
    // <span>2018-09-04</span>
    // </li>
    $.get("paging/auction",{currentPageStr:currentPage,type:type,sname:sname},function (auctions){
        if(type!=null){
            var bt="";
            if(type=="瓷器"){
                $("#lotbt1").addClass("on");
                $("#lotbt2").removeClass();
                $("#lotbt3").removeClass();
                $("#lotbt4").removeClass();
            }
            if(type=="玉器"){
                $("#lotbt1").removeClass();
                $("#lotbt2").addClass("on");
                $("#lotbt3").removeClass();
                $("#lotbt4").removeClass();
            }
            if(type=="字画"){
                $("#lotbt1").removeClass();
                $("#lotbt2").removeClass();
                $("#lotbt3").addClass("on");
                $("#lotbt4").removeClass();
            }
            if(type=="钱币"){
                $("#lotbt1").removeClass();
                $("#lotbt2").removeClass();
                $("#lotbt3").removeClass();
                $("#lotbt4").addClass("on");
            }

        }

        var lis = "";

        /*<li class="wow slideInUp" data-wow-delay="0.1s">
        <a href='/a/yuqi/66.html'style="height:170px" class="img">
        <img src="images/lot_01.jpg" style="height:170px" alt="清江西高安县厚生50两方宝"/>
        </a>
            <div class="title">
                  <h5 class="f14 pb5"><a href='/a/yuqi/66.html'>清江西高安县厚生50两方宝</a></h5>
                  <p class="f12 entitle">玉器</p>
                </div>
            <a class="jia"><span class="glyphicon glyphicon-plus"></span></a><i class="bor2"></i></li>*/
        for (var i = 0; i < auctions.list.length; i++) {
            var route = auctions.list[i];
            var li = '<li class="wow slideInUp" data-wow-delay="0.1s">\n'+
                    '<a href="jump/lot_xq?id='+route.id+'" style="height:170px" class="img">\n'+
                    '<img src="'+route.img+'" style="height:170px" alt="'+route.sname+'"/>\n'+
                    '</a>\n'+
                    '<div class="title">\n'+
                    '<h5 class="f14 pb5"><a href=\'/a/yuqi/66.html\'>'+route.sname+'</a></h5>\n'+
                    '<p class="f12 entitle">'+route.type+'</p>\n'+
                    '</div>\n'+
                    ' <a class="jia"><span class="glyphicon glyphicon-plus"></span></a><i class="bor2"></i></li>\n';
            lis+=li;
        }
        lis+='</ul>'

        $("#lotlistul").html(lis);

        window.scroll(0,0);


        var liss = "";
        var fristPage = '<li onclick="javascript:load('+1+',\''+type+'\',\''+sname+'\')"><a href="javascript:void(0)">首页</a></li>'

        var beforeNum = auctions.currentPage - 1;
        if(beforeNum<=0){
            beforeNum=1;
        }
        var beforePage = '<li class="threeword" onclick="javascript:load('+beforeNum+',\''+type+'\',\''+sname+'\')"><a href="javascript:void(0)">上一页</a></li>';
        liss += fristPage;
        liss += beforePage;

        // 定义开始位置begin,结束位置 end
        var begin; // 开始位置
        var end ; //  结束位置


        //1.要显示10个页码
        if(auctions.totalPage < 10){
            //总页码不够10页
            begin = 1;
            end = auctions.totalPage;
        }else{
            //总页码超过10页

            begin = auctions.currentPage - 5 ;
            end = auctions.currentPage + 4 ;

            //2.如果前边不够5个，后边补齐10个
            if(begin < 1){
                begin = 1;
                end = begin + 9;
            }
            //3.如果后边不足4个，前边补齐10个
            if(end > auctions.totalPage){
                end = auctions.totalPage;
                begin = end - 9 ;
            }
        }

        for(var i=begin; i<=end;i++){
            var li;
            if(auctions.currentPage==i){
                li = '<li class="curPage" onclick="javascript:load('+i+',\''+type+'\',\''+sname+'\')"><a href="javascript:void(0)">'+i+'</a></li>'
            }else {
                li = '<li onclick="javascript:load('+i+',\''+type+'\',\''+sname+'\')"><a href="javascript:void(0)">'+i+'</a></li>'
            }

            liss += li;
        }



        var lastPage = '<li class="threeword" onclick="javascript:load('+auctions.totalPage+',\''+type+'\',\''+sname+'\')"><a href="javascript:void(0)">末页</a></li>';
        var afterNum = auctions.currentPage+1;
        if(afterNum>=auctions.totalPage){
            afterNum =auctions.totalPage;
        }
        var nextPage = '<li class="threeword" onclick="javascript:load('+afterNum+',\''+type+'\',\''+sname+'\')"><a href="javascript:void(0)">下一页</a></li>';

        liss += nextPage;
        liss += lastPage;

        //将lis内容设置到 ul
        $("#pageNum").html(liss);
    });
}